package com.oymn.myapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.oymn.myapplication.R;
import com.oymn.myapplication.entity.Goods;
import com.oymn.myapplication.utils.MyImageView;
import com.oymn.myapplication.utils.Url;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

public class CartAdapter extends RecyclerView.Adapter<CartAdapter.MyViewHolder>{

    List<Goods> mDatas = new ArrayList<Goods>();
    String url = Url.goods_url;

    private Context context;
    private String user_phone;

    public CartAdapter(Context context,String user_phone){
        this.context = context;
        this.user_phone = user_phone;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        CartAdapter.MyViewHolder holder = new CartAdapter.MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cart,parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull CartAdapter.MyViewHolder holder, int position) {
        if(mDatas != null){
            holder.goodsName_1.setText(mDatas.get(position).getGoods_name());
            holder.goodsIntro_1.setText(mDatas.get(position).getGoods_introl());
            holder.price.setText(String.valueOf(mDatas.get(position).getGoods_price()));
        }

        //获取服务器图片
        String goods_id = String.valueOf(mDatas.get(position).getGoods_id());
        holder.imageView_1.setImageURL(url,goods_id);

        //移除购物车
        holder.yichu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //服务器删除商品列表项
                deleteItem(user_phone,holder.goodsName_1.getText().toString());
                //购物车页面更新
                mDatas.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeRemoved(position, getItemCount());
                Toast.makeText(context,"移除成功",Toast.LENGTH_SHORT).show();

            }
        });

        //结算商品
        holder.jiesuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction("android.intent.action.buy_goods");
                intent.putExtra("com.example.myapplication.buy_goods.inputPrice",holder.price.getText().toString());
                intent.putExtra("com.example.myapplication.buy_goods.inputName",holder.goodsName_1.getText().toString());
                intent.putExtra("com.example.myapplication.buy_goods.goods_img",holder.imageView_1.getDrawingCache());

                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }
    class MyViewHolder extends  RecyclerView.ViewHolder{

        TextView goodsName_1,goodsIntro_1,price;
        MyImageView imageView_1;
        Button yichu,jiesuan;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            goodsName_1 = itemView.findViewById(R.id.goodsName_1);
            goodsIntro_1 = itemView.findViewById(R.id.goodsIntro_1);
            price = itemView.findViewById(R.id.price);
            yichu = itemView.findViewById(R.id.yichu);
            jiesuan = itemView.findViewById(R.id.jiesuan);
            imageView_1 = itemView.findViewById(R.id.imageView_1);

        }
    }

    public void setmDatas(List<Goods> mDatas) {
        this.mDatas = mDatas;
        this.notifyDataSetChanged();
    }

    //通知服务器删除购物车商品
    public void deleteItem(String user_phone,String goods_name){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost request = new HttpPost(Url.cart_url);
                request.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

                //封转参数
                List<NameValuePair> param = new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("action", "deleteItem"));
                param.add(new BasicNameValuePair("user_phone", user_phone));
                param.add(new BasicNameValuePair("goods_name", goods_name));
                try {
                    request.setEntity(new UrlEncodedFormEntity(param, HTTP.UTF_8));
                    //获取响应的结果
                    HttpResponse response = httpClient.execute(request);
                    HttpEntity entity = response.getEntity();
                    //获取响应结果的信息
                    String result = EntityUtils.toString(entity, "UTF-8");
                    //没时间处理响应结果了，就这样吧
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

